<html>
<head><meta charset="utf-8"><title>use in impl blocks · t-lang · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/index.html">t-lang</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/use.20in.20impl.20blocks.html">use in impl blocks</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="236771845"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/use%20in%20impl%20blocks/near/236771845" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sebastian Malton <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/use.20in.20impl.20blocks.html#236771845">(Apr 30 2021 at 01:20)</a>:</h4>
<p>I ran into not being able to do the following:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">enum</span> <span class="nc">Foo</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">..</span><span class="p">.</span><span class="w"> </span><span class="p">}</span><span class="w"></span>

<span class="k">struct</span> <span class="nc">Bar</span><span class="p">;</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="n">Bar</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">use</span><span class="w"> </span><span class="n">Foo</span>::<span class="o">*</span><span class="p">;</span><span class="w"></span>

<span class="w">    </span><span class="k">fn</span> <span class="nf">new</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">..</span><span class="p">.</span><span class="w"> </span><span class="p">}</span><span class="w"></span>

<span class="w">    </span><span class="k">fn</span> <span class="nf">new_with_args</span><span class="p">(</span><span class="n">f</span>: <span class="kt">i32</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">..</span><span class="p">.</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>This complains with "error: <code>use</code> import is not supported in <code>trait</code>s or <code>impl</code>s".</p>
<p>Given that <code>use</code> is allowed in functions, I wonder why this is not allowed? It seems like with other blocks this should be allowed.</p>



<a name="236774241"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/use%20in%20impl%20blocks/near/236774241" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/use.20in.20impl.20blocks.html#236774241">(Apr 30 2021 at 01:54)</a>:</h4>
<p>Workaround is </p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">mod</span> <span class="nn">yolo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">use</span><span class="w"> </span><span class="k">super</span>::<span class="n">Foo</span>::<span class="o">*</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="k">impl</span><span class="w"> </span><span class="n">Bar</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="o">..</span><span class="p">.</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>I suspect it might not be supported because of questions about whether it should add associated items, especially if you think of something like <code>pub use Foo;</code>.</p>
<p>(And it's also not supported in <code>struct Foo { use Bar::*; ... }</code>, though that's different, but impls are also different from <code>fn</code>s, so it's always hard for me to argue analogies too strongly between different <code>{}</code>s.)</p>



<a name="236782539"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/use%20in%20impl%20blocks/near/236782539" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/use.20in.20impl.20blocks.html#236782539">(Apr 30 2021 at 04:02)</a>:</h4>
<p>You can keep the outer scope like:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">const</span><span class="w"> </span><span class="n">_</span>: <span class="p">()</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">use</span><span class="w"> </span><span class="n">Foo</span>::<span class="o">*</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="k">impl</span><span class="w"> </span><span class="n">Bar</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="c1">// ...</span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">};</span><span class="w"></span>
</code></pre></div>



<a name="236783026"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/use%20in%20impl%20blocks/near/236783026" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/use.20in.20impl.20blocks.html#236783026">(Apr 30 2021 at 04:09)</a>:</h4>
<p>Although I think <span class="user-mention" data-user-id="133169">@matklad</span> was arguing against <code>impl</code> in such positions for IDE's sake</p>



<a name="236786806"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/use%20in%20impl%20blocks/near/236786806" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/use.20in.20impl.20blocks.html#236786806">(Apr 30 2021 at 05:11)</a>:</h4>
<p><code>use</code> inside an impl block would mean you're defining an associated item to be the same as another item. E.g.</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">struct</span> <span class="nc">Struct</span><span class="p">;</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">new_struct</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">Struct</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">Struct</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="nb">Default</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">Struct</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">use</span><span class="w"> </span><span class="n">new_struct</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="n">default</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>I had an RFC for allowing <code>use</code> in <code>impl</code> to pull in items like you suggested, and it even got FCPed before somebody (I think petrochenkov) showed the meaning above.</p>



<a name="236786889"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/use%20in%20impl%20blocks/near/236786889" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/use.20in.20impl.20blocks.html#236786889">(Apr 30 2021 at 05:13)</a>:</h4>
<p>If I were to redo the RFC, I'd suggest adding <code>use clauses</code> wholesale to the language. E.g.</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">impl</span><span class="w"> </span><span class="n">Bar</span><span class="w"> </span><span class="k">use</span><span class="w"> </span><span class="n">Foo</span>::<span class="o">*</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="cm">/* */</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="236786963"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/use%20in%20impl%20blocks/near/236786963" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/use.20in.20impl.20blocks.html#236786963">(Apr 30 2021 at 05:14)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">bar</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">(</span><span class="n">t</span>: <span class="nc">T</span><span class="p">)</span><span class="w"> </span><span class="k">where</span><span class="w"> </span><span class="n">T</span>: <span class="nb">Default</span> <span class="nc">use</span><span class="w"> </span><span class="n">Foo</span>::<span class="n">Bar</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="cm">/* */</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="237016989"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/use%20in%20impl%20blocks/near/237016989" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sebastian Malton <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/use.20in.20impl.20blocks.html#237016989">(May 02 2021 at 01:46)</a>:</h4>
<p>So is <code>use new_struct as default</code> a thing currently?</p>



<a name="237028221"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/use%20in%20impl%20blocks/near/237028221" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> isHavvy <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/use.20in.20impl.20blocks.html#237028221">(May 02 2021 at 05:27)</a>:</h4>
<p>The code I showed doesn't exist. Just something that could exist if somebody put in the work.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>